package com.arif.app.sulamunnayirain;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.arif.alg.sulamunnayirain.Row;
import com.arif.alg.sulamunnayirain.SulamunNayyirain;
import com.arif.alg.sulamunnayirain.TableConstant;

public class ProcessActivity extends Activity{
	final String log_tag = "fikry_process";
	TableLayout tableLayout;
	
	 @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ConstantValue.sulamunNayyirain = new SulamunNayyirain();
		try {
			ConstantValue.sulamunNayyirain.process(1436, TableConstant.Table5._4_RabiulAkhir, 26);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setContentView(R.layout.activity_process);
		ScrollView sv =(ScrollView)findViewById(R.id.scrollViewProcess);
		
		tableLayout = new TableLayout(this);
		addTextView("Proses Perhitungan");
		addTextView("Total");
		String[] arrLabelTotal = {"Qo","Ja","Ba","Qo","Ja","Ba","Qo","Ja","Ba","Qo","Ja","Ba","Qo","Ja","Ba"};
		TableLayout tlTotal = convertFromRow(ConstantValue.sulamunNayyirain.getTotal(), arrLabelTotal);
		tableLayout.addView(tlTotal);		
		/********************************Takdil Khossoh********************************/
		String[] arrTakdilHossoh = {"ni","qoh","jah"};
		TableLayout tlTakdilKhossoh = convertFromRow(ConstantValue.sulamunNayyirain.getTakdilKhosoh(), arrTakdilHossoh);
		addTextView("Takdil Khossoh");
		tableLayout.addView(tlTakdilKhossoh);		
		
		/********************************Takdil Markaz********************************/
		//String[] arrTakdilHossoh = {"luts","ni","qoh"};
		String[] arrTakdilMarkaz = {"ni","qoh","jah"};
		TableLayout tlTakdilMarkaz= convertFromRow(ConstantValue.sulamunNayyirain.getTakdilMarkaz(), arrTakdilMarkaz);
		addTextView("Takdil Markaz");
		tableLayout.addView(tlTakdilMarkaz);		
		
		/********************************Al-Bu'du Ghoiru Muadal********************************/
		String[] arrGhoiruMuadal = {"ni","qoh","jah"};
		TableLayout tlGhoiruMuadal= convertFromRow(ConstantValue.sulamunNayyirain.getAlbuduGhairuMuaddal3(), arrGhoiruMuadal);
		addTextView("Al-Bu'du Ghoiru Muadal");
		tableLayout.addView(tlGhoiruMuadal);
		
		/********************************Takdil Syams********************************/
		String[] arrTakdilSyams = {"bu'","luts","ni","qoh","jah","brj"};
		TableLayout tlTakdilSyams = convertFromRow(ConstantValue.sulamunNayyirain.getTakdilSyams6(), arrTakdilSyams);
		addTextView("Takdil Syams");
		tableLayout.addView(tlTakdilSyams);
		
		/********************************Muqowamus Syams********************************/
		String[] arrMuqowamusSyams = {"bu'","luts","ni","qoh","jah","brj"};
		TableLayout tlMuqowamusSyams = convertFromRow(ConstantValue.sulamunNayyirain.getMuqowamusSyams6(), arrMuqowamusSyams);
		addTextView("Muqowamus Syams");
		tableLayout.addView(tlMuqowamusSyams);
		
		/********************************Takdil Ayam********************************/
		String[] arrTakdilAyam = {"luts","ni","qoh"};
		TableLayout tlTakdilAyam= convertFromRow(ConstantValue.sulamunNayyirain.getTakdilAyam(), arrTakdilAyam);
		addTextView("Takdil Ayam");
		tableLayout.addView(tlTakdilAyam);
		
		/********************************Al-Bu'du Muaddal********************************/
		String[] arrBukduMuaddal = {"khm","bu'","luts","ni","qoh","jah"};
		TableLayout tlBukduMuaddal = convertFromRow(ConstantValue.sulamunNayyirain.getAlbukdulMuaddal6(), arrBukduMuaddal);
		addTextView("Al-Bu'du Muaddal");
		tableLayout.addView(tlBukduMuaddal);
		
		/********************************Takdil Hosotus Saah********************************/
		String[] arrTakdilHosotusSaah = {"luts","ni","qoh","jah"};
		TableLayout tlTakdilHosotusSaah;
		try {
			tlTakdilHosotusSaah = convertFromRow(ConstantValue.sulamunNayyirain.getHisotusSaah6().getPerFragment(2, 4), arrTakdilHosotusSaah);
			addTextView("Takdil Hisotus Saah");
			tableLayout.addView(tlTakdilHosotusSaah);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		/********************************Takdil Alamah********************************/
		String[] arrTakdilAlamah = {"tsm","sub","sud","khm","bu'","luts","ni","qoh","saa","aym"};
		TableLayout tlTakdilAlamah = convertFromRow(ConstantValue.sulamunNayyirain.getTakdilAlamah10(), arrTakdilAlamah);
		addTextView("Takdil Alamah");
		tableLayout.addView(tlTakdilAlamah);
		
		/********************************Sa'atul Ijtima Alamat********************************/
		String[] arrSia = {"bu'","luts","ni","qoh","saa","aym"};
		TableLayout tlSia = convertFromRow(ConstantValue.sulamunNayyirain.getSaatulIjtima6(), arrSia);
		addTextView("Sa'atul Ijtima Alamat");
		tableLayout.addView(tlSia);
		
		/********************************Irtifaul Hilal********************************/
		String[] arrIrtifaulHilal = {"khm","bu'","luts","ni","qoh","jah"};
		TableLayout tlIrtifaulHilal = convertFromRow(ConstantValue.sulamunNayyirain.getIrtifaulHilal6(), arrIrtifaulHilal);
		addTextView("Irtifaul Hilal");
		tableLayout.addView(tlIrtifaulHilal);
		
		/********************************Muktsul Hilal********************************/
		String[] arrMuksulHilal = {"sud","khm","bu'","luts","ni","qoh","jah"};
		TableLayout tlMuksulHilal = convertFromRow(ConstantValue.sulamunNayyirain.getMuktsulHilal7(), arrMuksulHilal);
		addTextView("Muktsul Hilal");
		tableLayout.addView(tlMuksulHilal);
		
		tableLayout.setOrientation(1);
		sv.addView(tableLayout);
	}
	void addTextView(String text){
		TextView tv = new TextView(this);
		tv.setText(Html.fromHtml("<br>"+text));
		tv.setGravity(Gravity.CENTER);	    
		tableLayout.addView(tv);
	}
	TableLayout convertFromRow(Row row, String[] arrLabel){
		TableLayout tlTotal = new TableLayout(this);
		TableRow trTotal= new TableRow(this);
		TableRow trTotalLabel= new TableRow(this);		
		
		for(int i=0;i<row.length;i++){			
			TextView tv = new TextView(this);
			TextView tvLabel = new TextView(this);
			try {				
				tv.setText(row.get(i)+"");
				tvLabel.setText(arrLabel[i]);
				tvLabel.setWidth(22);
				tv.setWidth(22);
				tv.setGravity(Gravity.CENTER);
				tvLabel.setGravity(Gravity.CENTER);
				tvLabel.setBackgroundColor(Color.LTGRAY);
				Color color= new Color();				
					if(i%2==0){
						tv.setBackgroundColor(color.argb(150, 10, 0, 255));						
					}else{
						tv.setBackgroundColor(color.argb(190, 10, 0, 255));
					}					
				trTotal.addView(tv);
				trTotalLabel.addView(tvLabel);
				
			} catch (Exception e) {
				Toast t =  Toast.makeText(ProcessActivity.this, e.getMessage(), Toast.LENGTH_LONG);
				t.show();
				e.printStackTrace();
				return null;
			}			
		}		
		trTotal.setGravity(Gravity.CENTER);
		trTotalLabel.setGravity(Gravity.CENTER);		
		tlTotal.addView(trTotalLabel);
		tlTotal.addView(trTotal);		
		return tlTotal;
	}
}
